package com.marvels.financial.task.tasklet;

import java.util.Date;
import java.util.UUID;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.DateUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;
import com.marvels.financial.sms.service.PaymentCategorySmsService;

public class PaymentCategorySmsTasklet  implements Tasklet {

	@Autowired
	private PaymentCategorySmsService paymentCategorySmsService;
	
	@Value("${config.properties.path}")
	private  String configPropertiesPath;
	
	@Override
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
		try{
			String platCode=ServerProperties.getValue(configPropertiesPath, "application.properties", "platform.name.code");
			String key=platCode+"PAYMENT_CATEGORY_SMS";
			boolean isNotLock=CacheUtils.isNotLock(key, UUID.randomUUID().toString());
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>定时插入还款提醒短信开始,key"+key+",isNotLock"+isNotLock);
			if(isNotLock){//
				CacheUtils.setExpire(key, 180);//设置3分钟  服务器时间差距应该不会超过3分钟
				MarvelsLogUtil.getInstance().info(">>>>>>>>>>定时插入自动代扣失败提醒短信开始");
				paymentCategorySmsService.withholdFailureSms();
				MarvelsLogUtil.getInstance().info(">>>>>>>>>>定时插入自动代扣失败提醒短信结束");
				
				MarvelsLogUtil.getInstance().info(">>>>>>>>>>定时插入还款提醒短信开始");
				paymentCategorySmsService.repaymentReminderSms();
			}
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>定时插入还款提醒短信结束");
		} catch (Throwable e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>>还款短信提醒失败:",e);
			throw new Exception("定时插入还款类别短信异常",e);
		}
		return RepeatStatus.FINISHED;
	}

}
